package com.starmark.gateway.springcloud.provider.web;

import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * @author starmark
 * @since 2020/10/6 000622:29
 **/

@Controller
@RequestMapping("")
public class GatewayController {


    @RequestMapping(value={"redirect"})
    @ResponseBody
    public Mono<Void> redirect(ServerHttpResponse response
            , ServerWebExchange exchange) {
        return Mono.fromRunnable(() -> {
           String url= exchange.getAttributes().get("gateway_spec_url_redirect_url").toString();
            response.setStatusCode(HttpStatus.FOUND);
            response.getHeaders().setLocation(URI.create(url));
        });
    }

}
